package com.itheima.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.UmsResourceApi;
import com.itheima.api.UmsRoleResourceRelationApi;
import com.itheima.pojo.UmsResource;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-11:59
 */
@Service
public class UmsResourceService {

    @DubboReference
    private UmsResourceApi umsResourceApi;

    @DubboReference
    private UmsRoleResourceRelationApi umsRoleResourceRelationApi;


    /**
     * 添加新资源
     */
    public void create(UmsResource umsResource) {
        umsResourceApi.create(umsResource);
    }


    /**
     * 根据id删除资源
     */
    public void delete(Long id) {
        // 删除资源
        umsResourceApi.delete(id);
        // 删除资源与角色的关联关系
        umsRoleResourceRelationApi.deleteByResourceId(id);
    }


    /**
     * 分页模糊查询后台资源
     */
    public PageResult pageList(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
        Page<UmsResource> pageInfo = umsResourceApi.pageList(categoryId, nameKeyword, urlKeyword, pageNum, pageSize);
        return new PageResult<>(Convert.toInt(pageInfo.getCurrent()), Convert.toInt(pageInfo.getSize()), Convert.toInt(pageInfo.getTotal()), Convert.toInt(pageInfo.getCurrent()), pageInfo.getRecords());
    }


    /**
     * 查询所有后台资源
     */
    public List<UmsResource> listAll() {
        return umsResourceApi.list();
    }


    /**
     * 修改后台资源
     */
    public void update(Long id, UmsResource resource) {
        resource.setId(id);
        umsResourceApi.update(resource);
    }


    /**
     * 根据id获取资源详情
     */
    public UmsResource getById(Long id) {
        return umsResourceApi.getById(id);
    }
}
